package com.example.chao.component;

import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: WangYongChao
 * @Date: 2021/08/05/21:19
 * @Description: 时间日期处理
 */
@Component
public class DateManageUtil {
    /**
     * 时间格式转换，把时间段字符串变成时间
     * @param time
     * @return
     */
    public static List<Date> formatTime(String time) {
        //时间区间转换为起始时间和截止时间
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        String times[]=time.split(",|，|/|\n|\t"); //分隔符
        Date startDate=new Date();
        Date endDate=new Date();
        try{
            startDate=format1.parse(times[0]);
            endDate=format1.parse(times[1]);
        }catch (ParseException e) {
            e.printStackTrace();
        }
        //将起始时间定为指定日期的0时0分0秒0毫秒
        Calendar startTime = Calendar.getInstance();
        startTime.setTime(startDate);
        startTime.set(Calendar.HOUR_OF_DAY,0);//此时才是24h制
        startTime.set(Calendar.MINUTE , 0);
        startTime.set(Calendar.SECOND , 0);
        startTime.set(Calendar.MILLISECOND,0);
        startDate=startTime.getTime();
        //将起始时间定为指定日期的23时59分59秒999毫秒
        Calendar endTime = Calendar.getInstance();
        endTime.setTime(endDate);
        endTime.set(Calendar.HOUR_OF_DAY,23);//此时才是24h制
        endTime.set(Calendar.MINUTE , 59);
        endTime.set(Calendar.SECOND , 59);
        endTime.set(Calendar.MILLISECOND,999);
        endDate=endTime.getTime();
        List<Date> dateList=new ArrayList<>();
        dateList.add(startDate);
        dateList.add(endDate);
        return dateList;
    }

    /**
     * 时间格式转化
     * @param dateStr
     * @return
     */
    public static Date parseDate(String dateStr){
        Date date;
        try {
            if (dateStr.matches("[0-9]+")){ //判断是否为时间戳(纯数字判断)
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String sd = sdf.format(Long.parseLong(dateStr));
                date=sdf.parse(sd);
            }else {
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
                date=sdf.parse(dateStr);
            }
            return date;
        }catch (Exception e){
            return null;
        }
    }

    /**
     * 自定义时间格式
     * @param dateStr
     * @param format
     * @return
     * @throws ParseException
     */
    public static Date parseDates(String dateStr,String format) throws ParseException {
        Date date=null;
        java.text.DateFormat df=new SimpleDateFormat(format);
        String dt =dateStr;
        if ((!dt.equals(""))&&(dt.length()<format.length())){
            dt += format.substring(dt.length()).replaceAll("[YyMmDdHhSs]","0");
        }
        date=(Date) df.parse(dt);
        return date;
    }

    /**
     * 时间差（秒）
     * @param start
     * @param end
     * @return
     */
    public static long timeDifferenceSecond(Date start,Date end){
        Calendar c =Calendar.getInstance();
        c.setTime(start);
        long startTime=c.getTimeInMillis();
        c.setTime(end);
        long endTime=c.getTimeInMillis();
        long secondTimeDifference=(endTime-startTime)/1000;
        return secondTimeDifference;
    }

    /**
     * 时间差（分）
     * @param start
     * @param end
     * @return
     */
    public static long timeDifferenceMin(Date start,Date end){
        long secondTimeDifference=timeDifferenceSecond(start,end);
        return secondTimeDifference/60;
    }

    /**
     * 时间差（时）
     * @param start
     * @param end
     * @return
     */
    public static long timeDifferenceHours(Date start,Date end){
        long MinTimeDifference=timeDifferenceMin(start,end);
        return MinTimeDifference/60;
    }

    /**
     * 时间差（天）
     * @param start
     * @param end
     * @return
     */
    public static long timeDifferenceDays(Date start,Date end){
        long HoursTimeDifference=timeDifferenceHours(start,end);
        return HoursTimeDifference/24;
    }
}
